package aleetcode;

import java.util.Scanner;

public class _3 {
    public static void main(String[] args) {
        System.out.println("请输入一个罗马数:");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        int sum=romanToInt(str);
        System.out.println("罗马数"+str+"对应的整数是:"+sum);

    }
    public static int romanToInt(String s) {

        int n=s.length();
        int sum=gerNumber(s.charAt(n-1));
        for(int i=0;i<n-1;i++){
            if(gerNumber(s.charAt(i))>=gerNumber(s.charAt(i+1)))
                sum+=gerNumber(s.charAt(i));
            else
                sum-=gerNumber(s.charAt(i));
        }
        return sum;
    }
    public static int gerNumber(char ch){
        switch (ch){
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                return 0;
        }
    }
}
